package com.gxa.modules.community.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.community.entity.Region;
import com.gxa.modules.community.entity.RegionManage;
import com.gxa.modules.community.service.RegionService;
import com.gxa.modules.community.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "区域管理")

public class RegionManageController {
    @Autowired
    private RegionService regionService;

    @ApiOperation("区域管理 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "regionNameOrId",value ="区域名称/编号",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value ="小区id",dataType ="int"),
    }
    )
    @GetMapping("/region/list")
    public Result<List<RegionManage>> list01(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<RegionManage>> r=Result.success();
        String page1=(String) params.get("page");
        String limit1=(String)params.get("limit");
        Integer page=Integer.valueOf(page1);
        Integer limit=Integer.valueOf(limit1);
        try {
        PageHelper.startPage(page,limit);
        List<RegionManage> regionManages = this.regionService.queryByPage(params);
        PageInfo<RegionManage> pageInfo = new PageInfo<>(regionManages);
        long total = (int) pageInfo.getTotal();
        r=Result.success(regionManages,total);
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("分区区域下拉查询")
    @GetMapping("/regiontype/list")
    public Result<List<Region>> list03(){
        Result<List<Region>> r=Result.success();
        try {
            List<Region> regions = this.regionService.regiontypeList();
            r=Result.success(regions);
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("分区区域下拉 添加")
    @PostMapping("/regiontype/add")
    public Result<List<RegionManage>> regiontypeAdd(String regiontypeName){
        Result<List<RegionManage>> r=Result.success();
        try {
            this.regionService.regiontypeAdd(regiontypeName);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("区域管理 添加")
    @PostMapping("/region/add")
    public Result<List<RegionManage>> regionAdd(@RequestBody RegionManage regionManage){
        System.out.println(regionManage);
        Result<List<RegionManage>> r=Result.success();
        try {
            this.regionService.regionAdd(regionManage);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("区域管理 修改")
    @PutMapping("/region/update")
    public Result<List<RegionManage>> regionUpdate(@RequestBody RegionManage regionManage){
        Result<List<RegionManage>> r=Result.success();
        try {
            this.regionService.regionUpdate(regionManage);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("区域管理 删除")
    @DeleteMapping("/region/delete")
    public Result<List<RegionManage>> regionDelete( Integer id){
        Result<List<RegionManage>> r=Result.success();
        try {
            this.regionService.regionDelete(id);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("区域管理 批量删除")
    @DeleteMapping("/region/ids/delete")
    public Result<List<RegionManage>> regionDeleteIds(@RequestBody List<Integer> ids){
        Result<List<RegionManage>> r=Result.success();
        try {
            this.regionService.regionDeleteIds(ids);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
}
